<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>layuiAdmin 角色管理 iframe 框</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">
	</head>
	<body>
		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form test_edit" lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-card-body">
							<!-- <div class="layui-form-item">
								<label class="layui-form-label">角色</label>
								<div class="layui-input-block">
									<select name="rolename">
										<option value="0">管理员</option>
										<option value="1">超级管理员</option>
										<option value="2">纠错员</option>
										<option value="3">采购员</option>
										<option value="4">推销员</option>
										<option value="5">运营人员</option>
										<option value="6">编辑</option>
									</select>
								</div>
							</div> -->
							<input type="hidden" name="purview1" />
							<input type="hidden" name="purview_sub1"/>
							<div class="layui-form-item">
								<label class="layui-form-label">权限范围</label>
								<div class="layui-input-block">
									<div id="tree" class="demo-tree-more"></div>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">备注</label>
								<div class="layui-input-block">
									<textarea type="text" name="notes" autocomplete="off" class="layui-textarea"></textarea>
								</div>
							</div>
						</div>
					</div>

					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;">
								<!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
								<button class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>

		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/common.js" type="text/javascript" charset="utf-8"></script>
		<script>
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'tree', 'util'], function() {
				var $ = layui.$,
					tree = layui.tree,
					util = layui.util,
					form = layui.form;

				var _data = {
					id: getParam('id')
				}
				var purview = [],
					purview_sub = [];
				initdata();

				//监听提交
				form.on('submit(demo1)', function(data) {
					var data = data.field;
					_data = Object.assign(_data, data);

					var checkData = tree.getChecked('demoId1');

					purview = [];
					var inputlength = $('input[name=layuiTreeCheck]:checked');
					for (var i = 0; i < inputlength.length; i++) {
						purview.push($(inputlength[i]).val())
					}
					// console.log(inputlength);
					_data.purview = purview.join(',')
					_data.purview_sub = purview_sub.join(',')

					layer.confirm('确定提交数据？', function() {
						loading();
						if (_data.id) {
							submitdata(server.updatemember_role);
						} else {
							delete _data.id
							submitdata(server.addsite_rebate);
						}
					}, function() {
						layer.msg('取消操作');
					});

					return false;
				});

				function submitdata(_url) {
					$.post(_url, _data, function(res, status) {
						if (status == 'success') {
							layer.closeAll('loading');
							if (res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.parent.initdata();
									parent.layui.table.reload('test-table-operate');
								});
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				function initdata() {
					$.post(server.querymoduletree, {
						id: '5e0f6166a55311e9b8e2266e96755cf0,5e0f6166a55311e9b8e2266e96755cf1,5e0f6166a55311e9b8e2266e96710001,5e0f6166a55311e9b8e2266e96755cf2'
					}, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								var data = res.menu_list.data
								data[0].spread = true;
								//开启节点操作图标
								tree.render({
									elem: '#tree',
									data: data,
									showCheckbox: true,
									id: 'demoId1',
									// edit: ['add', 'update', 'del'],
									oncheck: function(obj) {
										var data = obj.data
										console.log(obj.checked);
										if(obj.checked){
											if(data.children){
												for (var i = 0; i < data.children.length; i++) {
													purview_sub.push(data.children[i].id)
												}
											} else {
												purview_sub.push(data.id)
											}
										} else {
											if(data.children){
												for (var i = 0; i < data.children.length; i++) {
													removearr(purview_sub,data.children[i].id)
												}
											} else {
												removearr(purview_sub,data.id);
											}
										}
										// console.log(purview_sub);
									},
								});
								
								var purviewval = $("input[name=purview1]").val().split(',');
								var purview_sub1 = $("input[name=purview_sub1]").val().split(',');
								purview = purviewval[0] ? purviewval : []
								// purview_sub = purview_sub1[0] ? purview_sub1 : [],
								// console.log(purview_sub);
								// console.log(purviewval.splice(1, purviewval.length));
								tree.setChecked('demoId1', purview_sub1)
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

			})
		</script>
	</body>
</html>
